#include "SComboBox.h"

#include <core/StyleMgr.h>

#include <QAbstractItemView>
#include <QColor>
#include <QHBoxLayout>
#include <QListView>

SComboBox::SComboBox(QWidget* parent /*= nullptr*/, QString strBkColor,
                     QString strBorderColor)
    : QComboBox(parent) {
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

  QWidget* container = static_cast<QWidget*>(view()->parent());
  container->setAttribute(Qt::WA_TranslucentBackground);
  container->setWindowFlag(Qt::FramelessWindowHint);
  container->setWindowFlag(Qt::NoDropShadowWindowHint);

  m_styleSheet =
      QString(
          "QComboBox						{color:#%1; "
          "padding: 4px 5px 4px 8px; border-radius:2px;border:1px solid #%3; "
          "background-color:#%2;}"
          "QComboBox:hover				{background-color:#%5;}"
          "QComboBox:on					{color:#%4;border:1px "
          "solid #%4;background-color:#%2;border-radius:2px;}"
          "QComboBox:disable				{color:#%7; border:1px "
          "solid #%7; background-color:#%2;}"
          "QComboBox::drop-down			"
          "{subcontrol-origin:padding;subcontrol-position:top right; "
          "border-radius:2px; width:20px;}"
          "QComboBox::down-arrow:on		"
          "{border-image:url(:/widgets/image/dropdown_up/"
          "dropdown_up_normal.png);}"
          "QComboBox::down-arrow			"
          "{border-image:url(:/widgets/image/dropdown_down/"
          "dropdown_down_normal.png);}"
          "QComboBox QAbstractItemView	{outline:0px;border:1px solid #%3; "
          "background-color:#%6;border-radius:2px;margin-top:2px;margin-bottom:"
          "2px;}"
          "QComboBox QAbstractItemView::item			"
          "{padding-left:4px;color:#%1;height:24px;background-color:#%6;}"
          "QComboBox QAbstractItemView::item:hover	{background-color:#%4;}"
          "QComboBox QAbstractItemView::item:selected	"
          "{color:#%4;background-image:url(:/widgets/image/dropdown_check/"
          "dropdown_check_pressed.png);background-origin:content;background-"
          "repeat:no-repeat;background-position:right;}")
          .arg(StyleMgr::instance().color("TextNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color(strBkColor).rgb(), 0, 16)
          .arg(StyleMgr::instance().color("BorderNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color(strBorderColor).rgb(), 0, 16)
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("BackNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("TipsBorder").rgb(), 0, 16);

  setStyleSheet(m_styleSheet);
  setView(new QListView());

  view()->setSelectionMode(QAbstractItemView::NoSelection);
}

SComboBox::~SComboBox() {}

void SComboBox::showPopup() {
  QComboBox::showPopup();
  m_showPopup = true;
}

void SComboBox::hidePopup() {
  QComboBox::hidePopup();
  m_showPopup = false;
}